package com.sso.oauth2.server.pojo;


import com.sso.oauth2.server.enums.ResultEnum;

/**
 * base response
 *
 * @author yins
 * @date 2021/05/19 09:41
 **/
public class BaseResp<T> {

    private Integer code;

    private String msg;

    private T data;

    private final static BaseResp OK = new BaseResp();
    private final static BaseResp FAIL = new BaseResp(ResultEnum.FAIL.code, ResultEnum.FAIL.msg);
    private final static BaseResp PARAM = new BaseResp(ResultEnum.PARAM.code, ResultEnum.PARAM.msg);
    private final static BaseResp ERROR = new BaseResp(ResultEnum.ERROR.code, ResultEnum.ERROR.msg);



    public BaseResp() {
        this.code = ResultEnum.OK.code;
        this.msg = ResultEnum.OK.msg;
    }

    private BaseResp(T data) {
        this.code = ResultEnum.OK.code;
        this.msg = ResultEnum.OK.msg;
        this.data = data;
    }

    private BaseResp(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private BaseResp(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }


    public static BaseResp ok() {
        return OK;
    }

    public static BaseResp fail() {
        return FAIL;
    }

    public static BaseResp param() {
        return PARAM;
    }

    public static BaseResp error() {
        return ERROR;
    }

    public static <T> BaseResp<T> ok(T data) {
        return new BaseResp(data);
    }

    public static BaseResp param(String msg) {
        return code(ResultEnum.PARAM.code, ResultEnum.PARAM.msg + ":" + msg);
    }

    public static BaseResp fail(String msg) {
        return code(ResultEnum.FAIL.code, msg);
    }

    public static BaseResp error(String msg) {
        return code(ResultEnum.ERROR.code, msg);
    }

    public static BaseResp code(Integer code, String msg) {
        return new BaseResp(code, msg);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
